package com.cljz.makepassenger.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.present.activity.MineP;
import com.cljz.makepassenger.utils.T;
import com.cljz.makepassenger.utils.date.GetSystemTimeUtil;
import com.cljz.makepassenger.widget.BlurImageView;
import com.cljz.makepassenger.widget.PubPopupWindow;
import com.cljz.makepassenger.widget.datetimpick.PersonTimeSelector;
import com.cljz.makepassenger.widget.pullzoomview.PullToZoomScrollViewEx;
import com.clzj.makepassenger.bean.UserInfo;
import com.jakewharton.rxbinding.view.RxView;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import com.zhy.autolayout.utils.L;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.router.Router;
import de.hdodenhof.circleimageview.CircleImageView;
import rx.functions.Action1;

/**
 * Created by apple on 2017/4/11.
 */

public class MineActivity extends BaseActivity<MineP> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.fl_back)
    FrameLayout flBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.ptz_scroll)
    PullToZoomScrollViewEx ptzScroll;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.fl_main)
    FrameLayout flMain;
    private LinearLayout llProfileHead;
    private CircleImageView civProfileHead;
    private BlurImageView ivZoom;
    private LinearLayout llNickname;
    private LinearLayout llSex;
    private LinearLayout llBirthday;
    private LinearLayout llSend;
    private LinearLayout llReceive;
    private TextView tvNickname;
    private TextView tvSex;
    private TextView tvBirthday;
    private String selectTime;
    private Map<String, String> userInfoMap;
    private Bitmap mBitmap;
    private int sexCode;

    @Override
    public void initData(Bundle savedInstanceState) {
        tvTitleName.setText("个人中心");
        llProfileHead = (LinearLayout) ptzScroll.getHeaderView().findViewById(R.id.ll_profile_head);
        civProfileHead = (CircleImageView) ptzScroll.getHeaderView().findViewById(R.id.civ_profile_head);
        ivZoom = (BlurImageView) ptzScroll.getZoomView().findViewById(R.id.iv_zoom);
        llNickname = (LinearLayout) ptzScroll.getmContentView().findViewById(R.id.ll_nickname);
        llSex = (LinearLayout) ptzScroll.getmContentView().findViewById(R.id.ll_sex);
        llBirthday = (LinearLayout) ptzScroll.getmContentView().findViewById(R.id.ll_birthday);
        llSend = (LinearLayout) ptzScroll.getmContentView().findViewById(R.id.ll_send);
        llReceive = (LinearLayout) ptzScroll.getmContentView().findViewById(R.id.ll_receive);
        tvNickname = (TextView) ptzScroll.getmContentView().findViewById(R.id.tv_nickname);
        tvSex = (TextView) ptzScroll.getmContentView().findViewById(R.id.tv_sex);
        tvBirthday = (TextView) ptzScroll.getmContentView().findViewById(R.id.tv_birthday);
        initPermission();
        Picasso.with(MineActivity.this).load(getP().getLocalUserInfo(getP().getToken()).getUser_logo()).into(new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                mBitmap = bitmap;
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }
        });
        userInfoMap = new HashMap<>();
        DisplayMetrics localDisplayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);
        LinearLayout.LayoutParams localObject = new LinearLayout.LayoutParams(localDisplayMetrics.widthPixels, (int) (7.0F * (localDisplayMetrics.widthPixels / 16.0F)));
        ptzScroll.setHeaderLayoutParams(localObject);
    }

    @Override
    protected void onResume() {
        super.onResume();
        changeUserInfo();
    }

    private void changeUserInfo() {
        UserInfo userInfo = getP().getLocalUserInfo(getP().getToken());
        Picasso.with(this).load(userInfo.getUser_logo()).placeholder(new BitmapDrawable(mBitmap)).into(civProfileHead);
        tvNickname.setText(userInfo.getUser_nickname());
        tvSex.setText("1".equals(userInfo.getUser_sex()) ? "男" : "女");
        selectTime = userInfo.getBirthday();
        tvBirthday.setText(userInfo.getBirthday());
        ivZoom.setBlurFactor(125);
        ivZoom.setDefaultDrawable(getResources().getDrawable(R.mipmap.touxiang));
        ivZoom.setBlurImageByUrl(userInfo.getUser_logo());

    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_mine;
    }

    @Override
    public MineP newP() {
        return new MineP();
    }

    @Override
    protected void initListener() {
        super.initListener();
        RxView.clicks(flBack)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                finish();
            }
        });
        RxView.clicks(llProfileHead)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                Intent intentPhoto = new Intent(MineActivity.this, UserLogoActivity.class);
                intentPhoto.putExtra(UserLogoActivity.EXTRA_SHOW_CAMERA, true);
                intentPhoto.putExtra(UserLogoActivity.EXTRA_SELECT_COUNT, 1);
                intentPhoto.putExtra(UserLogoActivity.EXTRA_SELECT_MODE, UserLogoActivity.MODE_SINGLE);
                startActivity(intentPhoto);
            }
        });
        RxView.clicks(llNickname)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                Router.newIntent((Activity) App.getInstance()).to(NicknameEditActivity.class).launch();
            }
        });
        RxView.clicks(llSex)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                new PubPopupWindow(MineActivity.this, new PubPopupWindow.SexSelect() {
                    @Override
                    public void select(int position) {
                        sexCode = (position == 0 ? 1 : 2);
                        userInfoMap.clear();
                        userInfoMap.put("token", getP().getLocalUserInfo(getP().getToken()).getToken());
                        userInfoMap.put("user_sex", String.valueOf(sexCode));
                        getP().getUserMsgUp(userInfoMap, MineP.UpUserInfo.SEX);
                    }
                }).showAtLocation(rlTitle, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
            }
        });
        RxView.clicks(llBirthday)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                new PersonTimeSelector(MineActivity.this, new PersonTimeSelector.ResultHandler() {
                    @Override
                    public void handle(String time) {
                        showLoadingDialog();
                        selectTime = time;
                        userInfoMap.clear();
                        userInfoMap.put("token", getP().getLocalUserInfo(getP().getToken()).getToken());
                        userInfoMap.put("birthday", selectTime);
                        L.e(selectTime);
                        getP().getUserMsgUp(userInfoMap, MineP.UpUserInfo.BIRTHDAY);
                    }
                }, GetSystemTimeUtil.getStart100Time(), GetSystemTimeUtil.getStartTime(), GetSystemTimeUtil.getTime(GetSystemTimeUtil.getDatetime(selectTime))).show();
            }
        });
        RxView.clicks(llSend)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                startActivity(new Intent(MineActivity.this, InviteListActivity.class).putExtra("type", 1));
            }
        });
        RxView.clicks(llReceive)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                startActivity(new Intent(MineActivity.this, InviteListActivity.class).putExtra("type", 2));
            }
        });

    }

    public void userMsgUpFail(String error) {
        dismissLoadingDialog();
        T.getInstance().showShort(error);
    }

    public void userMsgUpSucceed(String msg) {
        dismissLoadingDialog();
        T.getInstance().showShort(msg);
        changeUserInfo();
    }

}
